/*
 * name:动态验证码 DynamicCheckCode
 * description:验证组件
 *             主要为手机和邮箱验证码，通过用户发起请求后发送
 * author:yanmu.wj@taobao.com
 * date:2011-11-23
 */
KISSY.add('DynamicCheckCode',function(S,Tip,TimeoutBtn){
    var DynamicCheckCode = function(cfg){
        this.trigger = cfg.trigger && S.one(cfg.trigger);
        this.triggerTip = cfg.triggerTip && S.one(cfg.triggerTip) ? Tip(cfg.triggerTip) : null;
        this.input = cfg.input && S.one(cfg.input);
        this.inputTip = cfg.inputTip && S.one(cfg.inputTip) ? Tip(cfg.inputTip) : null;
        this.getUrl = cfg.getUrl || '';
        this.checkUrl = cfg.checkUrl || '';
        this.data = cfg.data || '';

        this.defaultMsg = {error:'验证码发送失败，请重试！',ok:'验证码已发送，请查收！'}
        this.msg = cfg.msg || this.defaultMsg;

        this.tobtn = 0;

        this.disabledMsg = S.isString(cfg.disabledMsg) ? cfg.disabledMsg : '验证码格式有误，请重新输入！';
        this.disabled = true;
    };
    var _DynamicCheckCode = function(cfg){
        return new DynamicCheckCode(cfg);
    };
    S.augment(DynamicCheckCode,{
        init:function(){
            if(!this.trigger || !this.input || !this.getUrl) return this;

            var self = this;

            //初始化TimeoutBtn
            this.tobtn = TimeoutBtn({
                btn:this.trigger,
                time:60,//每次验证码获取间隔1分钟
                format:'%t%秒后重新获取'
            });

            //获取验证码
            this.trigger.on('click',function(){
                self.getCode();
            });

            //校验验证码
            this.input.on('keyup blur',function(){
                self.checkCode();
            });

            return this;
        },
        /*
         * 获取验证码
         */
        getCode:function(){
            var self = this;

            S.io({
                url:self.getUrl,
                data:{data:S.trim(self.data)},
                type:'post',
                dataType:'json',
                success:function(data){
                    if(data.success){
                        this.disalbed = false;
                        if(self.triggerTip) self.triggerTip.ok(data.msg || this.msg.ok || this.defaultMsg.ok);
                        self.input.prop('disabled',false);
                        this.tobtn.start();
                    }else{
                        this.disabled = true;
                        this.disabledMsg = '请重新获取验证码！';
                        if(self.triggerTip) self.triggerTip.error(data.msg || this.msg.error || this.defaultMsg.error);
                        self.input.prop('disabled',true);
                    }
                },
                error:function(){
                    this.disabled = true;
                    this.disabledMsg = '请重新获取验证码！';
                }
            });
        },
        checkCode:function(){
            var self = this;

            //可用性校验
            if(this.checkAble()){
                self.disabled = false;
                self.inputTip.ok();
            }else{
                self.disabled = true;
                self.inputTip.error('验证码格式有误，请重新输入！');
            }

            if(!this.checkUrl) return;

            S.io({
                url:self.checkUrl,
                data:{code:S.trim(self.input.val())},
                type:'post',
                dataType:'json',
                success:function(data){
                    if(data.success){
                        self.disalbed = false;
                        self.inputTip.ok(data.msg || '');
                    }else{
                        self.disabled = true;
                        self.disabledMsg = '验证码格式有误，请重新输入！';
                        self.inputTip.error(data.msg || self.disabledMsg);
                    }
                },
                error:function(){
                }
            });
        },
        checkAble:function(){
            return !!S.trim(this.input.val()).match(/^\d{6}$/);
        },
        reset:function(){
            if(!this.trigger || !this.input || !this.getUrl) return;
            this.disabled = true;
            this.tobtn.clear();
            this.input.val('');
            this.triggerTip && this.triggerTip.hide();
            this.inputTip && this.inputTip.hide();
        }
    });

    return _DynamicCheckCode;
},{
    requires:['Message','TimeoutBtn']
});
